package com.book.servlet.page;

import com.alibaba.fastjson.JSON;
import com.book.entity.Student;
import com.book.entity.User;
import com.book.service.StudentService;
import com.book.service.impl.StudentServiceImpl;
import com.book.util.ThymeleafUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.thymeleaf.context.Context;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/*
 **@Author:youpeng
 **Created on 2022/4/25
 */
@WebServlet("/students")
public class StudentServlet extends HttpServlet {

    StudentService studentService=new StudentServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Context context = new Context();
        User user = (User) req.getSession().getAttribute("user");
        context.setVariable("nickname",user.getNickname());
        String msg = req.getParameter("msg");
        String pageNum = req.getParameter("page");
        int num=pageNum==null?1:Integer.parseInt(pageNum);
        Page<Student> page = PageHelper.startPage(num, 5);
        List<Student> studentListLike = studentService.getStudentListLike(msg == null ? "" : msg);
        context.setVariable("studentList",studentListLike);
        PageInfo<Student> pageInfo = new PageInfo<>(studentListLike,5);
        context.setVariable("pageInfo",pageInfo);
        ThymeleafUtil.process("students.html",context, resp.getWriter());
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String msg = req.getParameter("msg");
        req.getSession().setAttribute("msg",msg);
        this.doGet(req,resp);
    }
}
